package com.demo.controller;

import com.demo.bean.Lesson;
import com.demo.bean.User;
import com.demo.service.LessonService;
import com.demo.service.RedisService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @author 李天航
 * 每周活动的对外接口
 */
@RestController
@RequestMapping("/lesson")
public class LessonController {
    @Resource
    LessonService lessonService;
    @Resource
    RedisService redisService;

    /**
     * 添加每周活动
     * @param lesson 每周活动的信息
     * @param session 缓存
     * @return status,msg,如果有冲突会有addLesson和ConflictLesson
     */
    @PostMapping("/add")
    public Object addLesson(Lesson lesson, HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        lesson.setUserAccount(user.getUserAccount());
        return lessonService.addLesson(lesson);
    }

    /**
     * 删除每周活动
     * @param lessonId 活动编号
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/delete")
    public Object deleteLesson(Integer lessonId,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return lessonService.deleteLesson(lessonId,user.getUserAccount());
    }

    /**
     * 更新每周活动
     * @param lesson 更新后的活动（不需要全部）
     * @param session 缓存
     * @return status,msg,如果有冲突会有addLesson和ConflictLesson
     */
    @PostMapping("/update")
    public Object updateLesson(Lesson lesson,HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        lesson.setUserAccount(user.getUserAccount());
        return lessonService.updateLesson(lesson);
    }

    /**
     * 查找所有的每周活动
     * @param session 缓存
     * @return status,msg
     */
    @PostMapping("/getall")
    public Object getAllLessons(HttpSession session)
    {
        User user=(User)  redisService.getSessionData(session.getId(),"user");
        return lessonService.getLessonsByUserAccount(user.getUserAccount());
    }

}
